package guru.mercurio.core.delivery;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class CGIHelper{

	private static HttpURLConnection cgiConn(String surl) throws Exception{
		HttpURLConnection conn;
		URL url = new URL(surl);
		URLConnection urlConn = url.openConnection();
		urlConn.setDoOutput(true);
		conn = (HttpURLConnection)urlConn;
		return conn;
	}

	public static void mercurioCgi(String surl) throws Exception{
		HttpURLConnection conn = cgiConn(surl);
		conn.setRequestMethod("POST");
		conn.connect();
		conn.disconnect();
	}

	public static String mercurioRfcCgi(String surl) throws Exception{
		HttpURLConnection  conn = cgiConn(surl);

		conn.setRequestMethod("GET");

		InputStreamReader isr = new InputStreamReader(conn.getInputStream()); //Se empiezan a preparar las estructuras para recibir la respuesta
		BufferedReader br = new BufferedReader(isr);
		String temp;
		String tempResponse = "";

		while ((temp = br.readLine()) != null) //Se recibe la respuesta
			tempResponse = tempResponse + temp;
		String response = tempResponse;
		br.close();
		isr.close();
		conn.disconnect();
		return response;
	}

	public static String buildURL(String[] args){
		String urls = new String(args[0]+"?function=recibeSMS&clave="+args[1]+"&codPaisRem="+args[2]+"&codAreaRem="+args[3]+"&numRem="+args[4]+"&mensaje="+args[5]+"&idMensaje="+args[6]);
		while (!(urls.indexOf(" ")==-1))
			urls = urls.substring(0,urls.indexOf(" "))+"%20"+urls.substring(urls.indexOf(" ")+1,urls.length());
		return urls;

	}

	public static String buildURL(String url, String clave, int codPaisRem, int codAreaRem, int numRem, String mensaje, String idMensaje){
		String urls = new String(url+"?clave="+clave+"&codPaisRem="+codPaisRem+"&codAreaRem="+codAreaRem+"&numRem="+numRem+"&mensaje="+mensaje+"&idMensaje="+idMensaje);
		while (!(urls.indexOf(" ")==-1)){
			urls = urls.substring(0,urls.indexOf(" "))+"%20"+urls.substring(urls.indexOf(" ")+1,urls.length());
		}
		return urls;
	}

	public static String buildURL(String url, String pasaporte, String password, int codPaisDest, int codAreaDest, int numDest, String mensaje){
		String urls = new String(url+"?pasaporte="+pasaporte+"&password="+password+"&codPaisDest="+codPaisDest+"&codAreaDest="+codAreaDest+"&numDest="+numDest+"&mensaje="+mensaje);
		while (!(urls.indexOf(" ")==-1))
			urls = urls.substring(0,urls.indexOf(" "))+"%20"+urls.substring(urls.indexOf(" ")+1,urls.length());
		return urls;
	}

	public static String buildURL(String url, String pasaporte, String password, String codPaisDest, String codAreaDest, String numDest, String mensaje){
		String urls = new String(url+"?pasaporte="+pasaporte+"&password="+password+"&codPaisDest="+codPaisDest+"&codAreaDest="+codAreaDest+"&numDest="+numDest+"&mensaje="+mensaje);
		while (!(urls.indexOf(" ")==-1))
			urls = urls.substring(0,urls.indexOf(" "))+"%20"+urls.substring(urls.indexOf(" ")+1,urls.length());
		return urls;
	}


	public static int checkXML(String xml){
		if (!(xml.indexOf("<?xml version="+(char)34+"1.0"+(char)34+"?>")==-1)){
			xml = xml.substring(xml.indexOf(">")+1);
			System.out.println(xml);
			if (!(xml.indexOf("<Mensaje>")==-1)){
				xml = xml.substring(xml.indexOf(">")+1);
				System.out.println(xml);
				if(!(xml.indexOf("</Mensaje>")==-1))
					return(0);
			}
		}
		return 1;
	}

	/*
	 * precondicion: Se ha ejecutado previamente checkXML y dio 0
	 */
	public static String getMessage(String xml){
		return xml.substring(xml.indexOf("<Mensaje>")+9,xml.indexOf("</Mensaje>"));
	}

}